package com.hitrust.gs.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import system.LoginContext;
import system.data.ProductTypeBase;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductBidProvideCodeBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;

import framework.core.ConstDefine;

public class GSProductBidDao extends GSBaseDAO{

	public final static String runhuayou = "20";
	public final static String luntai = "17";
	private final String insertSql = " insert into t_product_bid (" +
	"product_id,"+
	"product_code,"+
	"product_type,"+
	"provider_code,"+
	"remark,"+
	"create_time,"+
	"create_user,"+
	"create_user_id,"+
	"last_update_time,"+
	"last_update_user,"+
	"last_update_user_id,"+
	"product_name,"+
	"status,"+
	"id"+
	") values (?,?,?,?,?,?,?,?,?,?,?,?,?,(select sys_guid() from dual))";	
	
	private final String updateSql = " update t_product_bid set " +
	"product_id =?,"+
	"product_code = ?,"+
	"product_type = ?,"+
	"provider_code = ?,"+
	"remark = ?,"+
	"create_time= ?,"+
	"create_user = ?,"+
	"create_user_id = ?,"+
	"last_update_time = ?,"+
	"last_update_user = ?,"+
	"last_update_user_id = ?,"+
	"product_name = ?,"+
	"status = ? " +
	"where id = ?";
	
	private final String updateByProductCodeSql = " update t_product_bid set " +
	"product_id =?,"+
	"product_code = ?,"+
	"product_type = ?,"+
	"provider_code = ?,"+
	"remark = ?,"+
	"create_time= ?,"+
	"create_user = ?,"+
	"create_user_id = ?,"+
	"last_update_time = ?,"+
	"last_update_user = ?,"+
	"last_update_user_id = ?,"+
	"product_name = ?,"+
	"status = ? " +
	" where product_code = ?";
	
	/**
	 * 新增产品供应商分类,无连接,不做事务用
	 * @param 实体参数
	 * @return
	 * @throws GSException
	 */
	public boolean insert (GSProductBidBean vo) throws GSException{
		String tSql = this.insertSql;
		Object[] tobj = new Object[13];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getProductName());
        tobj[12] = String.valueOf(vo.getStatus());
		try {
			return this.executeUpdate(tSql, tobj);
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 新增产品供应商分类，有连接，做事务用
	 * @param 实体参数
	 * @param conn
	 * @return
	 * @throws GSException
	 */
	public boolean insertByConnection (GSProductBidBean vo,Connection conn) throws GSException{
		String tSql = this.insertSql;
		Object[] tobj = new Object[13];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getProductName());
        tobj[12] = String.valueOf(vo.getStatus());
		try {
			return this.executeUpdate(conn,tSql, tobj);
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 修改产品供应商分类，无连接，非事务用
	 * @param 实体类
	 * @return
	 * @throws GSException
	 */
	public boolean update (GSProductBidBean vo) throws GSException{
		String tSql = this.updateSql;
		Object[] tobj = new Object[14];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getProductName());
        tobj[12] = String.valueOf(vo.getStatus());
        tobj[13] = vo.getId();
		try {
			return this.executeUpdate(tSql,tobj,null ,"");
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 修改产品供应商分类，有连接，做事务用
	 * @param vo
	 * @param conn
	 * @return
	 * @throws GSException
	 */
	public boolean updateByConnection (GSProductBidBean vo,Connection conn) throws GSException{
		String tSql = this.updateSql;
		Object[] tobj = new Object[14];
        tobj[0] = vo.getProductId();
        tobj[1] = vo.getProductCode();
        tobj[2] = String.valueOf(vo.getProductType());
        tobj[3] = vo.getProviderCode();
        tobj[4] = vo.getRemark();
        tobj[5] = vo.getCreateTime();
        tobj[6] = vo.getCreateUser();
        tobj[7] = vo.getCreateUserId();
        tobj[8] = vo.getLastUpdateTime();
        tobj[9] = vo.getLastUpdateUser();
        tobj[10] = vo.getLastUpdateUserId();
        tobj[11] = String.valueOf(vo.getProductName());
        tobj[12] = String.valueOf(vo.getStatus());
        tobj[13] = vo.getId();
		try {
			return this.executeUpdate(conn,tSql,tobj,null ,"");
		} catch (Exception e) {
			throw new GSException(GSException.E20042,GSException.E20042_MSG);
		}
	};
	
	/**
	 * 根据ID查询产品供应商分类
	 * @param Id
	 * @return
	 * @throws GSException
	 */
	public boolean findProductBidById (String Id) throws GSException{
		String tSql = " select * from t_product_bid where id = ?";
		boolean tFlag = false;
		Object[] object = {Id};
		try {
			ArrayList tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				tFlag=true;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return tFlag;
	}
	
	/**
	 * 根据产品编码查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductBidBean findProductBidByProuductCode (String code) throws GSException{
		String tSql = " select * from t_product_bid where product_code = ? ";
		ArrayList tList ;
		Object[] object = {code};
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductBidBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	public Map findAllProductBid () throws GSException{
		String tSql = " select * from t_product_bid where status = ? ";
		ArrayList tList ;
		Object[] object = {"1"};
		Map map= new HashMap();
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList!=null&&tList.size() > 0){
            	for(int i=0;i<tList.size();i++){
            		GSProductBidBean	bean = (GSProductBidBean)tList.get(i);
            		map.put(""+bean.getProductCode(),""+bean.getProductType());
            	}
            }
			tList=null;
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return map;
	}
	/**
	 * 根据产品编码查询产品供应商分类,有效的
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductBidBean findProductBidByProuductCodeStatus (String code) throws GSException{
		String tSql = " select * from t_product_bid where product_code = ? and status = 1";
		ArrayList tList ;
		Object[] object = {code};
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductBidBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品编码查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductBidBean findProductBidByProuductCodeNoStauts (String code) throws GSException{
		String tSql = " select * from t_product_bid where product_code = ? ";
		ArrayList tList ;
		Object[] object = {code};
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductBidBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品编码查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public GSProductBidBean findProductBidByProuductId (String id) throws GSException{
		String tSql = " select * from t_product_bid where product_id = ?";
		ArrayList tList ;
		Object[] object = {id};
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				return (GSProductBidBean) tList.get(0);
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品类型查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public ArrayList findProductBidByType (String type) throws GSException{
		String tSql = " select * from t_product_bid where product_type = ?";
		ArrayList tList ;
		Object[] object = {type};
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				return tList;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return null;
	}
	
	/**
	 * 根据产品类型查询产品供应商分类。有效的
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public ArrayList findProviderCodeByType (String type) throws GSException{
		String tSql = " select distinct(provider_code) from t_product_bid where product_type = ? and status=1";
		ArrayList tList = new ArrayList();
		Object[] object = {type};
		try {
			tList = this.executeQuery(new GSProductBidProvideCodeBean(), tSql, object);
			if(tList.size()>0){
				return tList;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return tList;
	}
	
	/**
	 * 根据产品类型查询产品供应商分类
	 * @param 产品编码
	 * @return
	 * @throws GSException
	 */
	public boolean isProductBidByType (String type) throws GSException{
		String tSql = " select * from t_product_bid where product_type = ?";
		ArrayList tList ;
		Object[] object = {type};
		try {
			tList = this.executeQuery(new GSProductBidBean(), tSql, object);
			if(tList.size()>0){
				return true;
			}
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return false;
	}
	
	/**
	 * 根据供应商编码，产品供应商分类删除产品供应商
	 * @param providerCode
	 * @param conn
	 * @param 如果为空或者为NULL为只根据供应商编码删除，
	 * @return
	 * @throws GSException
	 */
	public boolean delete(String providerCode,Connection conn,String type) throws GSException{
		
		String tSql = "";
		boolean flag =false;
		Object[] object = {providerCode};
		if(type.equals("")||type==null){
			tSql = " delete from t_product_bid where provider_code = ? ";	
		}else{
			object = new Object[]{providerCode, type};
			tSql = " delete from t_product_bid where provider_code = ? and product_type = ?";			
		}	
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 根据供应商编码，产品供应商分类删除产品供应商,逻辑删除
	 * @param providerCode
	 * @param conn
	 * @param 如果为空或者为NULL为只根据供应商编码删除，
	 * @return
	 * @throws GSException
	 */
	public boolean deleteByStatus(String providerCode,Connection conn,String type) throws GSException{
		
		String tSql = "";
		boolean flag =false;
		Object[] object = {ConstDefine.STR_VALID_NO, providerCode};
		if(type.equals("")||type==null){
			tSql = " update t_product_bid set status = ? where provider_code = ? ";	
		}else{
			object = new Object[]{ConstDefine.STR_VALID_NO,providerCode, type};
			tSql = " update t_product_bid set status = ? where provider_code = ? and product_type = ?";			
		}	
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean deletePrudctBidByStatus(String type,Connection conn) throws GSException
	{
		String tSql = " delete from t_product_bid where product_type = ?";
		boolean flag =false;
		Object[] object = {type};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctBidOtherByStatus(String type,Connection conn) throws GSException
	{
		String tSql = " update t_product_bid set product_type = "+GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID+" where product_type = ?";
		boolean flag =false;
		Object[] object = {type};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean deletePrudctBidByProductCode(String productCode,Connection conn) throws GSException
	{
		String tSql = " delete from t_product_bid where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(conn,tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctBidStatusByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_bid set status = 0 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 修改产品招标分类，状态为1，类型为99
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctBidStatusAndTypeByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_bid set status = 1,product_type=99 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 修改产品招标分类，状态为1，类型为99,isBid变成99
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctBidStatusAndTypeAndIsBidByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_bid set status = 1,product_type=99 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 将产品招标分类表的status改为1
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctIsBidStatusAndTypeAndIsBidByProductCode(String productCode) throws GSException
	{
		String tSql = " update t_product_bid set status = 1 where product_code = ?";
		boolean flag =false;
		Object[] object = {productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	/**
	 * 修改产品招标分类并将status改为1
	 * @param productCode
	 * @return
	 * @throws GSException
	 */
	public boolean updatePrudctBidAndStatus(String productType,String productCode) throws GSException
	{
		String tSql = " update t_product_bid set status = 1,product_type=? where product_code = ?";
		boolean flag =false;
		Object[] object = {productType,productCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}
	
	public boolean updatePrudctBidStatusByApepCode(String apepCode) throws GSException
	{
		String tSql = " update t_product_bid set status = 0 where APEP_CODE = ?";
		boolean flag =false;
		Object[] object = {apepCode};
		try {
			flag = this.executeUpdate(tSql, object);
			
		} catch (Exception e) {
			 throw new GSException(GSException.E19999,GSException.E19999_MSG);
		}
		return flag;
	}

}
